<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 角色管理 -->
          <el-tab-pane label="角色管理">
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="dialogVisible = true"
                >新增角色</el-button
              >
            </el-row>
            <!-- 给表格绑定数据 -->
            <el-table border="" :data="list">
              <el-table-column
                label="序号"
                width="120"
                align="center"
                type="index"
              />
              <el-table-column
                label="角色名称"
                width="240"
                align="center"
                prop="name"
              />
              <el-table-column label="描述" align="center" prop="description" />
              <el-table-column label="操作" align="center">
                <!-- 作用域插槽 -->
                <template slot-scope="{ row }">
                  <el-button
                    size="small"
                    type="success"
                    @click="assignPerm(row.id)"
                    >分配权限</el-button
                  >
                  <el-button
                    size="small"
                    type="primary"
                    @click="roleEadit(row.id)"
                    >编辑</el-button
                  >
                  <el-button
                    size="small"
                    type="dange"
                    @click="deleteRole(row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <el-pagination
                layout="prev,pager,next"
                :total="page.total"
                :page-size="page.pagesize"
                :current-page="page.page"
                @current-change="changePage"
              ></el-pagination>
            </el-row>
          </el-tab-pane>
          <!-- 公司信息 -->
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon:closable="false"
            />
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input
                  disabled
                  style="width: 400px"
                  v-model="formData.name"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  disabled
                  style="width: 400px"
                  v-model="formData.companyAddress"
                />
              </el-form-item>
              <el-form-item label="电话">
                <el-input
                  disabled
                  style="width: 400px"
                  v-model="formData.companyPhone"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  disabled
                  style="width: 400px"
                  v-model="formData.mailbox"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  disabled
                  style="width: 400px"
                  type="textarea"
                  :rows="3"
                  v-model="formData.remarks"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 弹窗组件 -->
      <el-dialog title="编辑角色" :visible="dialogVisible" @close="cancel">
        <el-form
          label-width="120px"
          :model="roleData"
          :rules="rules"
          ref="roleData"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleData.name"></el-input>
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleData.description"></el-input>
          </el-form-item>
        </el-form>
        <!-- 放置footer插槽 -->
        <el-row type="flex" justify="center">
          <el-col :span="8">
            <el-button size="small" @click="cancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOk"
              >确定</el-button
            >
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 放置一个给角色添加权限的弹窗 -->
      <el-dialog
        title="分配权限"
        :visible="showPermDialog"
        @close="btnPermCancel"
      >
        <!-- 权限是一棵树   -->
        <el-tree
          :data="permData"
          :props="defaultPerms"
          :default-expand-all="true"
          :show-checkbox="true"
          :check-strictly="true"
          node-key="id"
          :default-checked-keys="permIds"
          ref="permTree"
        />
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6" type="flex" justify="center">
            <el-button size="small" type="primary" @click="btnPermOK"
              >确定</el-button
            >
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getRoleList,
  getCompanyInfo,
  deleteRole,
  updateRole,
  getRoleDetail,
  addRole,
  assignPerm,
} from "@/api/setting";
import { transListToTreData } from "@/utils";
import { getPermissionList } from "@/api/permission";
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      dialogVisible: false,
      showPermDialog: false,
      list: [],
      page: {
        //专门来存放分页的数据
        page: 1,
        pagesize: 12,
        total: 0,
      },
      formData: {
        //公司信息
      },
      //角色信息
      roleData: {
        name: "",
        description: "",
      },
      rules: {
        name: [
          { required: true, message: "角色名称不能为空", trigger: "blur" },
        ],
      },
      permData: [], //专门用来接收权限数据
      defaultPerms: { label: "name" }, //定义显示的字段名称和子属性的字段名称
      rolesId: null, //用来记录当前分配权限的id
      permIds: [], //默认选择的权限点勾选数组
    };
  },
  computed: {
    ...mapGetters(["companyId"]),
  },
  created() {
    this.getRoleList(); //获取角色数据
    this.getCompanyInfo(); //获取公司信息
  },
  methods: {
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page);
      this.page.total = total;
      this.list = rows;
    },
    //改变页面数据
    changePage(newPage) {
      //newPage当前点击的页码
      this.page.page = newPage; //将当前页码赋值给当前的最新页码
      this.getRoleList();
    },
    //获取用户信息的方法
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId);
    },
    //删除角色
    async deleteRole(id) {
      try {
        await this.$confirm("确认删除该角色吗？");
        await deleteRole(id);
        this.getRoleList();
        this.$message.success("删除角色成功");
      } catch (err) {
        console.log(err);
      }
    },
    //编辑角色
    async roleEadit(id) {
      this.roleData = await getRoleDetail(id);
      this.dialogVisible = true;
    },
    //取消
    cancel() {
      this.roleData = {
        name: "",
        description: "",
      };
      this.$refs.roleData.resetFields();
      this.dialogVisible = false;
    },
    //确定
    async btnOk() {
      try {
        await this.$refs.roleData.validate();
        //这里需要考虑到新增的场景
        if (this.roleData.id) {
          //编辑场景
          await updateRole(this.roleData);
        } else {
          //新增
          await addRole(this.roleData);
        }
        this.getRoleList(); //重新更新下页面角色数据
        this.$message.success("操作成功");
        this.dialogVisible = false;
      } catch (err) {
        console.log(err);
      }
    },
    //分配权限按钮
    async assignPerm(id) {
      this.permData = transListToTreData(await getPermissionList(), "0"); //转换成一个对象树形数据结构
      this.rolesId = id;
      const { permIds } = await getRoleDetail(id); //permIds是当前角色拥有的权限点数据
      this.permIds = permIds;
      this.showPermDialog = true;
    },
    //分配权限的确定
    async btnPermOK() {
      //调用el-tree的方法 得到所有勾选上的数据
      // console.log(this.$refs.permTree.getCheckedNodes());

      await assignPerm({
        permIds: this.$refs.permTree.getCheckedKeys(),
        id: this.rolesId,
      });
      this.$message.success("当前分配权限成功");
      this.showPermDialog = false;
    },
    btnPermCancel() {
      this.permIds = [];
      this.showPermDialog = false;
    },
  },
};
</script>

<style>
</style>
